package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {



    //website消费调用  trip-member-api/server  里面接口/实现类
    //消费者与生产者间调用  或者服务与服务间调用
    @Reference //需要调用发布到zookeeper中服务都使用这个
    private IUserInfoService userInfoService;

    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return  !userInfoService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){


        userInfoService.sendVerifyCode(phone);

        return  new AjaxResult();
    }


    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname,String password, String rpassword, String verifyCode){
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return  AjaxResult.SUCCESS;
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletResponse response, String username, String password){

        String token = userInfoService.userLogin(username, password);

        //将token封装cookie中, 通过response对象返回到浏览器中
        CookieUtil.addCookie(response, "token", token, RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());

        return  AjaxResult.SUCCESS;
    }





}
